cbuffer cbFixed
{
	float gFogStart = 500.0f;
	float gFogRange = 100.0f;
	float3 gFogColor = {0.7f, 0.7f, 0.7f};
}

Texture2D diffuseTexture : BLEND <
	string name = "seafloor.dds"; 
	string UIName = "Diffuse Texture";
>;

Texture2D Texture1 : TEX1 <
	string name = "seafloor.dds"; 
	string UIName = "Texture1";
>;

Texture2D Texture2 : TEX2 <
	string name = "seafloor.dds"; 
	string UIName = "Texture2";
>;

Texture2D Texture3 : TEX3 <
	string name = "seafloor.dds"; 
	string UIName = "Texture3";
>;
	
SamplerState diffuseSampler
{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = WRAP;
	AddressV = WRAP;
};
SamplerState tex1Sampler
{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = WRAP;
	AddressV = WRAP;
};
SamplerState tex2Sampler
{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = WRAP;
	AddressV = WRAP;
};
SamplerState tex3Sampler
{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = WRAP;
	AddressV = WRAP;
};

matrix g_World : World;
matrix g_WorldInv : WorldInverseTranspose;
matrix g_WVP : WorldViewProjection;
matrix g_ViewInv : ViewInverse;


struct VS_INPUT
{
    float3 Pos          : POSITION;         //position
    float3 Norm		: NORMAL;     // Normal
    float2 Tex          : TEXCOORD0;        //texture coordinate
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex1 : TEXCOORD0;
    float2 Tex2 : TEXCOORD1;
    float fogLerp : FOG;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;
    float4 hPos = float4(input.Pos, 1);
    output.Pos = mul(hPos, g_WVP);
    output.Tex1 = input.Tex;
    output.Tex2 = input.Tex * 24.0f;
    
    float d = distance(output.Pos, float3(0.0f, 0.0f, 0.0f));
    output.fogLerp = saturate((d - gFogStart) / gFogRange);
    
    return output;
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------

float4 PS( PS_INPUT input) : SV_Target
{
	float3 col1 = Texture1.Sample(tex1Sampler, input.Tex2).rgb;
	float3 col2 = Texture2.Sample(tex2Sampler, input.Tex2).rgb;
	float3 col3 = Texture3.Sample(tex3Sampler, input.Tex2).rgb;
	
	float3 blendcol = diffuseTexture.Sample(diffuseSampler, input.Tex1).rgb;
	float totalinverse = 1.0f / (blendcol.r + blendcol.g + blendcol.b);
	
	col1 *= blendcol.r * totalinverse;
	col2 *= blendcol.g * totalinverse;
	col3 *= blendcol.b * totalinverse;
	
	float3 litColor = (col1 + col2 + col3).rgb;
	//float3 foggedColor = lerp(litColor, gFogColor, input.fogLerp);
	
	return float4(litColor, 1.0f);
	
}

RasterizerState EnableCulling
{
    CullMode = Back;
    //FillMode = Wireframe;
    FrontCounterClockwise = false;
};

//--------------------------------------------------------------------------------------
// Technique
//--------------------------------------------------------------------------------------
technique10 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
        
        SetRasterizerState(EnableCulling); 
    }
}

